import  java.util.Random;

public class Deck 
{
	int k=0,i,j;
	public Card[] deck=new Card[52];
	public int index=0;
	
	
	Deck()//Constructs a deck with all possible cards, ordered by figure
	{
		for (i=2;i<15;i++)
		{
			for(j=0;j<4;j++)
			{
				deck[k]=new Card(i,j);
				k++;
			}
		}
	}
	
	Card getCard()//Gives a card from the top of the deck; Don't use shuffleDeck() after a card had been given out!!! Not yet implemented;
	{
		this.index++;
		return this.deck[this.index-1];
	}
	
	String getCardName()//Alternative to the method above; Returns only the 'name' of a card; A bit indirect
	{
		this.index++;
		return this.deck[this.index-1].showCard();
	}
	
	
	void shuffleDeck()//Best used a few times; shuffleDeck() resets the deck, therefore can be used straight away after one turn is resolved
	{
		int i,j=1,k=10,x;
		Random roll=new Random();//Generator of random numbers
		Card[] shufflePile0= new Card[52];
		Card[] shufflePile1= new Card[52];
		Card[] shufflePile2= new Card[52];
		Card[] shufflePile3= new Card[52];
		int[] counter={0,0,0,0}; //Each index counts a number of cards in the corresponding shufflePile
		this.index=0;//Resets the deck.
		
		while(k>=0)//Shuffled 10 times for once is not enough; k could be given by a player (void shuffleDeck(int k)) - thought to think about
		{
			k--;
			for (i=0;i<52;i++) //Puts a card from the deck on top of a random shufflePile
			{
				x=roll.nextInt(4); 
				//System.out.println(x+"x");
				switch(x)
				{
					case 0:
						shufflePile0[counter[0]]=this.deck[i];
						this.deck[i]=null;
						//System.out.println(shufflePile0[counter[0]].showCard()+" here");
						//System.out.println(counter[0]+" elo");
						counter[0]++;
						break;
					case 1:
						shufflePile1[counter[1]]=this.deck[i];
						this.deck[i]=null;
						counter[1]++;
						break;
					case 2:
						shufflePile2[counter[2]]=this.deck[i];
						this.deck[i]=null;
						counter[2]++;
						break;
					case 3:
						shufflePile3[counter[3]]=this.deck[i];
						this.deck[i]=null;
						counter[3]++;
						break;
				}
			}
		
			/*while(counter[0]>0)
			{
				System.out.println(shufflePile0[counter[0]-1].showCard());
				counter[0]--;
			}
			System.out.println("end of Pile0");*/
		
			for (i=0;i<52;i++)//Takes cards from shufflePiles to create a new, shuffled deck
			{
				x=i%4;
				while (counter[x]==0)
				{
					//System.out.println(counter[x]);
					x=(i+j)%4; 
					j++;
				}
				j=1; 
				switch (x)
				{
					case 0:
						deck[i]=shufflePile0[counter[0]-1];
						//shufflePile0[counter[0]-1]=null;
						counter[0]--;
						//System.out.println(deck[i].showCard()+" dp0");
						break;
					case 1:
						deck[i]=shufflePile1[counter[1]-1];
						//shufflePile1[counter[1]]=null;
						counter[1]--;
						//System.out.println(deck[i].showCard()+" dp1");
						break;
					case 2:
						deck[i]=shufflePile2[counter[2]-1];
						//shufflePile2[counter[2]]=null;
						counter[2]--;
						//System.out.println(deck[i].showCard()+" dp2");
						break;
					case 3:
						deck[i]=shufflePile3[counter[3]-1];
						//shufflePile3[counter[3]]=null;
						counter[3]--;
						//System.out.println(deck[i].showCard()+" dp3");
						break;
				}
			}
		}
	}

}
